/*-
 *  Copyright (C) 2009 Peter Baldwin   
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.peterbaldwin.client.android.sms2email;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class EmailAddressAdapter extends ResourceCursorAdapter {

	private static final boolean ECLAIR = Integer.parseInt(Build.VERSION.SDK) >= 5;

	private static final Uri CONTENT_URI = Uri
			.parse(ECLAIR ? "content://com.android.contacts/data/emails"
					: "content://contacts/contact_methods/email");

	private static final String _ID = "_id";
	private static final String NAME = ECLAIR ? "display_name" : "name";
	private static final String DATA = ECLAIR ? "data1" : "data";
	private static final String TIMES_CONTACTED = "times_contacted";

	private static final String[] PROJECTION = { _ID, NAME, DATA };
	private static final int COLUMN_INDEX_NAME = 1;
	private static final int COLUMN_INDEX_DATA = 2;

	/**
	 * Show most contacted entries first, then sort by name.
	 */
	private static final String SORT_ORDER = String.format("%s DESC, %s ASC",
			TIMES_CONTACTED, NAME);

	private final ContentResolver mContentResolver;

	public EmailAddressAdapter(Context context) {
		super(context, R.layout.recipient_dropdown_item, null);
		mContentResolver = context.getContentResolver();
	}

	@Override
	public final String convertToString(Cursor cursor) {
		String address = cursor.getString(COLUMN_INDEX_DATA);
		return address;
	}

	@Override
	public final void bindView(View view, Context context, Cursor cursor) {
		TextView text1 = (TextView) view.findViewById(android.R.id.text1);
		TextView text2 = (TextView) view.findViewById(android.R.id.text2);
		String name = cursor.getString(COLUMN_INDEX_NAME);
		String address = cursor.getString(COLUMN_INDEX_DATA);
		text1.setText(name);
		text2.setText(address);
	}

	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
		String where = null;
		String[] selectionArgs = null;

		if (constraint != null) {
			where = String.format("(%s LIKE ?) OR (%s LIKE ?)", NAME, DATA);
			String filter = String.valueOf(constraint) + '%';
			selectionArgs = new String[] { filter, filter };
		}

		return mContentResolver.query(CONTENT_URI, PROJECTION, where,
				selectionArgs, SORT_ORDER);
	}
}
